﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using System.Data;
using System.Data.SqlClient;

namespace baitaptuan5
{
    public partial class BaoCaoCongNo : Form
    {
        public BaoCaoCongNo()
        {
            InitializeComponent();
        }
        DataTable dtkhachhang = new DataTable();
        DataTable TableCongNo = new DataTable();
        DataTable dtTinhPhieuThu = new DataTable();
        int indexToanCuc;

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            base.Dispose();            
        }

        
        
        private void BaoCaoCongNo_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedItem  ="4"; // chua xu ly 
            string m = this.comboBox1.SelectedItem.ToString();

            KhachHangBUS khbus = new KhachHangBUS();
            
            dtkhachhang = khbus.LayTable();
            this.dtgBaoCaoCongNo.DataSource = dtkhachhang;

            //Phieu Thu 
            PhieuThuBUS ptBUS = new PhieuThuBUS();
            dtTinhPhieuThu = ptBUS.LayTableTheoThang(m);
            this.dataGridView2.DataSource = dtTinhPhieuThu;

            // Tính hóa đơn của từng khách hàng
            
            DataTable dtTinhHoaDon = khbus.LayTableNoHoaDonCuaTungKhachHangTheoThang(m);
            this.dataGridView1.DataSource = dtTinhHoaDon;          
            
            int sumrowTinhHoaDon = dtTinhHoaDon.Rows.Count;
            int sumrowTinhPhieuThu = dtTinhPhieuThu.Rows.Count;

            if (sumrowTinhHoaDon >= sumrowTinhPhieuThu)
            {
                int r = 0; //dong cua datagridview 
                foreach (DataRow rowTinhHoaDon in dtTinhHoaDon.Rows)
                {
                    foreach (DataRow rowPhieuThu in dtTinhPhieuThu.Rows)
                    {
                        this.dtgBaoCaoCongNo.Rows[r].Cells["PhatSinh"].Value = float.Parse(rowTinhHoaDon["TongGiaTriMoiHoaDon"].ToString());
                        if (rowTinhHoaDon["maKhachHang"].ToString() == rowPhieuThu["MaKhachHang"].ToString())
                        {
                            this.dtgBaoCaoCongNo.Rows[r].Cells["PhatSinh"].Value = float.Parse(rowPhieuThu["TongTienTra"].ToString()) - float.Parse(rowTinhHoaDon["TongGiaTriMoiHoaDon"].ToString());
                        }
                        this.dtgBaoCaoCongNo.Rows[r].Cells["nodau"].Value = float.Parse(this.dtgBaoCaoCongNo.Rows[r].Cells["nocuoi"].Value.ToString()) - float.Parse(this.dtgBaoCaoCongNo.Rows[r].Cells["phatsinh"].Value.ToString());
                    }
                    r++;
                }
            }
            else
            {
                int r = 0; //dong cua datagridview
                foreach (DataRow rowPhieuThu in dtTinhPhieuThu.Rows)
                {
                    foreach (DataRow rowTinhHoaDon in dtTinhHoaDon.Rows)
                    {
                        this.dtgBaoCaoCongNo.Rows[r].Cells["PhatSinh"].Value = float.Parse(rowPhieuThu["TongTienTra"].ToString());
                        if (rowTinhHoaDon["maKhachHang"].ToString() == rowPhieuThu["MaKhachHang"].ToString())
                        {
                            this.dtgBaoCaoCongNo.Rows[r].Cells["PhatSinh"].Value = float.Parse(rowPhieuThu["TongTienTra"].ToString()) - float.Parse(rowTinhHoaDon["TongGiaTriMoiHoaDon"].ToString());
                        }
                        this.dtgBaoCaoCongNo.Rows[r].Cells["nodau"].Value = float.Parse(this.dtgBaoCaoCongNo.Rows[r].Cells["nocuoi"].Value.ToString()) - float.Parse(this.dtgBaoCaoCongNo.Rows[r].Cells["phatsinh"].Value.ToString());
                    }
                    r++;
                }
            }
         
            // Tô Màu Begin
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dtgBaoCaoCongNo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dtgBaoCaoCongNo.DefaultCellStyle.SelectionBackColor = Color.Red;
            this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red;
            this.dataGridView2.DefaultCellStyle.SelectionBackColor = Color.Red;
            //if (indexToanCuc < 0 || indexToanCuc > this.dataGridView1.Rows.Count ) 
            //    return;
            //else
            //this.dataGridView1.Rows[indexToanCuc].Selected = true;
            this.dtgBaoCaoCongNo.Rows[0].Selected = false;
            this.dataGridView1.Rows[0].Selected = false;
            this.dataGridView2.Rows[0].Selected = false;
            int indexToanCuc1 = indexToanCuc ;
            int indexToanCuc2 = indexToanCuc;
            int indexToanCuc3 = indexToanCuc;
            bool indexToanCuc1_flag = true;
            bool indexToanCuc2_flag = true;
            bool indexToanCuc3_flag = true;
            if (this.indexToanCuc < 0 || indexToanCuc > this.dataGridView1.Rows.Count - 1)
            {
                indexToanCuc1_flag = false;
                indexToanCuc1 = 0;
            }
            if (this.indexToanCuc < 0 || indexToanCuc > this.dtgBaoCaoCongNo.Rows.Count - 1)
            {
                indexToanCuc2 = 0;
                indexToanCuc2_flag = false;
            }
            if (this.indexToanCuc < 0 || indexToanCuc > this.dataGridView2.Rows.Count - 1)
            {
                indexToanCuc3_flag = false;
                indexToanCuc3 = 0;
            }
                this.dataGridView1.Rows[indexToanCuc1].Selected = indexToanCuc1_flag;
                this.dtgBaoCaoCongNo.Rows[indexToanCuc2].Selected = indexToanCuc2_flag;
                this.dataGridView2.Rows[indexToanCuc3].Selected = indexToanCuc3_flag;

            // Tô Màu Begin


            // Đổ dữ liệu vào   BEGIN
                //foreach (DataGridViewColumn col in dtgBaoCaoCongNo.Columns)
                //{
                //    TableCongNo.Columns.Add(col.Name);
                //}

                //foreach (DataGridViewRow rows in dtgBaoCaoCongNo.Rows)
                //{
                //    DataRow row = TableCongNo.NewRow();
                //    row[0] = rows.Cells[0].Value;
                //    row[1] = rows.Cells[1].Value;
                //    row[2] = rows.Cells[2].Value;
                //    row[3] = rows.Cells[3].Value;
                //    row[4] = rows.Cells[4].Value;
                //    row[5] = rows.Cells[5].Value;
                //    row[6] = rows.Cells[6].Value;
                //    TableCongNo.Rows.Add(row);
                //    //lUu du lieu tu datagridview vao data table  END
                //}
            // Đổ dữ liệu vào   END
            
        }


        private void  dtgBaoCaoCongNo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            // ----------------------- To Mau 
            if (e.RowIndex < 0)
                return;

            this.dtgBaoCaoCongNo.SelectedRows[0].DefaultCellStyle.SelectionBackColor = Color.Red;
            // ----------------------- To Mau 

            // To màu 3 bảng 
            this.indexToanCuc= this.dtgBaoCaoCongNo.SelectedRows[0].Index;
            BaoCaoCongNo_Load(sender,e);
            // To màu 3 bảng 
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            this.dataGridView1.SelectedRows[0].DefaultCellStyle.SelectionBackColor = Color.Red;
            
            this.indexToanCuc = this.dataGridView1.SelectedRows[0].Index;
            BaoCaoCongNo_Load(sender, e);

        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            this.dataGridView2.SelectedRows[0].DefaultCellStyle.SelectionBackColor = Color.Red;

            this.indexToanCuc = this.dataGridView2.SelectedRows[0].Index;
            BaoCaoCongNo_Load(sender, e);
        }

     

        private void button1_Click(object sender, EventArgs e)
        {
            ReportDB.exportDataToExcel("Báo Cáo Tồn Tháng 5", TableCongNo);
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            ReportDB.exportDataToExcel("Báo Cáo Tồn Tháng 5", dtTinhPhieuThu);
        }

        private void bÁOCÁOCÔNGNỢToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //lUu du lieu tu datagridview vao data table BEGIn

            foreach (DataGridViewColumn col in dtgBaoCaoCongNo.Columns)
            {
                TableCongNo.Columns.Add(col.Name);
            }

            foreach (DataGridViewRow rows in dtgBaoCaoCongNo.Rows)
            {
                DataRow row = TableCongNo.NewRow();
                row[0] = rows.Cells[0].Value;
                row[1] = rows.Cells[1].Value;
                row[2] = rows.Cells[2].Value;
                row[3] = rows.Cells[3].Value;
                row[4] = rows.Cells[4].Value;
                row[5] = rows.Cells[5].Value;
                row[6] = rows.Cells[6].Value;
                TableCongNo.Rows.Add(row);
                //lUu du lieu tu datagridview vao data table  END
            }

            ReportDB.exportDataToExcel("Báo Cáo Tồn Tháng 5", TableCongNo);
        }

        private void tổngTiềnThanhToánHóaĐơnMỗiKHToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReportDB.exportDataToExcel("Báo Cáo Tồn Tháng 5", TableCongNo);
        }

        private void tổngTiềnTrảMỗiKháchHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ReportDB.exportDataToExcel("Báo Cáo Tồn Tháng 5", dtTinhPhieuThu);
        }


        //private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        //{
        //    MessageBox.Show("chang"+this.comboBox1.SelectedItem.ToString());
        //}

        private void button2_Click(object sender, EventArgs e)
        {
            //lUu du lieu tu datagridview vao data table BEGIn

            foreach (DataGridViewColumn col in dtgBaoCaoCongNo.Columns)
            {
                TableCongNo.Columns.Add(col.Name);
            }

            foreach (DataGridViewRow rows in dtgBaoCaoCongNo.Rows)
            {
                DataRow row = TableCongNo.NewRow();
                row[0] = rows.Cells[0].Value;
                row[1] = rows.Cells[1].Value;
                row[2] = rows.Cells[2].Value;
                row[3] = rows.Cells[3].Value;
                row[4] = rows.Cells[4].Value;
                row[5] = rows.Cells[5].Value;
                row[6] = rows.Cells[6].Value;
                TableCongNo.Rows.Add(row);
                //lUu du lieu tu datagridview vao data table  END
            }

            ReportDB.exportDataToExcel("Báo Cáo Tồn Tháng 5", TableCongNo);
        }

        private void thoátBCCôngNợToolStripMenuItem_Click(object sender, EventArgs e)
        {

            this.Dispose();
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            BaoCaoCongNo_Load(sender, e);
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }


    }
}
